<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            备件入库
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">库位</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="选择/扫描 库位" id="workCellCode" value=""
                            onclick="getIdSelector();" />
                    </div>
                    <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWorkCell()"></i>
                </div>
            </li>
            <li class="aui-list-item" onclick="goSelectmrlCode()">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">物料</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="选择物料" id="mrlCode" disabled value=""
                            onclick="getIdSelector();" />
                    </div>
                    <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectmrlCode()"></i>
                </div>
            </li>

            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">物料名称</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="mrlName" disabled="disabled" />
                    </div>
                </div>
            </li>

            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">批次号</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="输入批次号" id="lotCode" value="" onclick="timePicker();" />
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">数量</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="输入数量" id="qty" value="" onclick="getIdSelector();" />
                    </div>
                </div>
            </li>
        </ul>

        <div style="width:100%;margin-top:2%;"></div>
        <!-- <div class="aui-btn aui-btn-info" style="width:40%;margin-left:4%;" onclick="save()">入库</div>
        <div class="aui-btn aui-btn-info" style="width:40%;margin-left:4%;" onclick="doSearch()">查询</div> -->
        <div class="aui-btn aui-btn-info" style="display: flex;  
        justify-content: center;
        align-items:center;width:45%;margin-left:2%;margin-bottom:2%;height: 40px;font-size: 18px;float: left;"
            onclick="save()">入库</div>
        <div class="aui-btn aui-btn-info" style="display: flex;  
        justify-content: center;
        align-items:center;width:45%;margin-left:2%;height: 40px;font-size: 18px;float: left;margin-bottom:2%;"
            onclick="doSearch()">查询</div>
        <!-- <div style="background-color:white;width: 100%;margin-top:2%;margin-bottom:20%; ">
            <table class="table" style='width:100%;' id="table">
                <thead>
                    <tr style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 12px;font-weight: bold;">
                        <td algin='center'>物料号</td>
                        <td algin='center'>物料名</td>
                        <td algin='center'>批次号</td>
                        <td algin='center'>库存数</td>
                        <td algin='center'>仓库编码</td>
                        <td algin='center'>库位编码</td>
                        <td style="display:none;">物料名称</td>
                        <td style="display:none;">规格型号</td>
                        <td style="display:none;">供应商</td>
                    </tr>
                </thead>
                <tbody id="tbodyHtml">
                </tbody>
            </table>
        </div> -->
        <div style="background-color:white; height: 100%;width: 100%;margin-top:15%;margin-bottom:20%;" id="tbodyHtml">

        </div>
    </div>

    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var workCellGid = ""; //库位GID
    var workCellCode = ""; //库位编码
    var workCellName = ""; //库位名称
    var mrlCode = "";
    //初始化
    apiready = function () {
        $("#workCellCode").focus();
        idSelector = "workCellCode";

    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {
            //alert(ret);扫码的结果都是编码
            //1：仓库
            if (idSelector == 'workCellCode') {

                $("#workCellCode").val(ret);
                //查询库位
                getWorkCell(ret, idSelector);

            }
        });
    }

    //5:库位扫描校验
    function getWorkCell(code, inputId) {
        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getWorkCell";
        var data = {
            values: {
                flag: "bj_rk_ck", //库内库间标识（out_kn库内  out_kj库间,in_rk入库出库）
                workCellCode: code, //库位编码
                appVersion: api.appVersion,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    var list = eval(myobj.list);
                    workCellGid = list[0].workCellGid;
                    workCellCode = list[0].workCellCode;
                    workCellName = list[0].workCellName;
                    document.getElementById(inputId).value = list[0].workCellCode + ":" + list[0].workCellName;
                    $("#mrlCode").val("");
                    getMrlCode();
                    doSearch();
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    $("#workCellCode").focus();
                    $("#workCellCode").select();
                }
            }
        });
    }

    //6：选择库位
    function goSelectWorkCell() {
        api.openWin({
            name: 'uwmSelectWorkCell_bj',
            url: 'uwmSelectWorkCell_bj.html',
            pageParam: {
                flag: "bj_rk_ck",
                name: "wmSpareInOfStock",
            }
        });
    }

    //库位选择后回调函数
    function setWorkCell(id, code, name) {
        workCellGid = id;
        workCellCode = code;
        workCellName = name;
        document.getElementById('workCellCode').value = code + ":" + name;
        $("#mrlCode").val("");
        getMrlCode();
    }

    //7；PDA自带扫描回车事件-库位
    $("#workCellCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {
            //仓库编码
            $("#workCellCode").val(sm);
            //校验改箱码是否存在
            getWorkCell(sm, "workCellCode");

        }
    });

    //6：选择物料
    function goSelectmrlCode() {
        api.openWin({
            name: 'uwmSelectMrlCodeNew',
            url: 'uwmSelectMrlCodeNew.html',
            pageParam: {
                name: "wmSpareInOfStock",
                workCellGid: workCellGid,
            }
        });
    }

    //库位选择后回调函数
    function setMrlCode(mrlCode, sysid) {
        mrlCode = mrlCode;
        document.getElementById('mrlCode').value = mrlCode;
        getMrlName(sysid)

    }

    function getMrlName(sysid) {
        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getMrlName";
        var data = {
            values: {
                sysid: sysid,
                appVersion: api.appVersion,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    document.getElementById('mrlName').value = myobj.mrlName;
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //查询
    function doSearch() {

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/doSearchStock_bj";
        var data = {
            values: {
                workCellGid: workCellGid,
                workCenterGid: "",
                appVersion: api.appVersion,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //返回成功
                if (myobj.code == "0") {
                    var list = eval(myobj.list);
                    var html = "";
                    for (var i = 0; i < list.length; i++) {
                        // html += "<tr style='text-align: center;color: black;font-size: 15px;background-color: #F5F5F5;height:66px;'>";
                        // html += "<td name='mrlCode'>" + list[i].mrlCode + "</td>";
                        // html += "<td name='mrlName'>" + list[i].mrlName + "</td>";
                        // html += "<td name='lotCode'>" + list[i].lotCode + "</td>";
                        // html += "<td name='qty'>" + list[i].qty + "</td>";
                        // html += "<td name='workCenterCode'>" + list[i].workCenterCode + "</td>";
                        // html += "<td name='workCellCode'>" + list[i].workCellCode + "</td>";
                        // html += "<td name='mrlName' style='display:none;'>" + list[i].mrlName + "</td>";
                        // html += "<td name='specification' style='display:none;'>" + list[i].specification + "</td>";
                        // html += "<td name='supplierName' style='display:none;'>" + list[i].supplierName + "</td>";
                        // html += "</tr>";
                        html += "<div class='aaui-btn-info aui-btn-block' style='margin-bottom: 10px;color: black !important;background-color: #addbef !important;height: 130px;padding: 0.2rem 0.2rem;font-size: 15px;'>";
                        html += "<table style='width:100%;'>";
                        html += "<tr style='width:100%;'><td style='width:100%;' colspan='2'>物料号：" + list[i].mrlCode + "</td></tr>";
                        html += "<tr style='width:100%;'><td style='width:100%;' colspan='2'>物料名：" + list[i].mrlName + "</td></tr>";
                        html += "<tr style='width:100%;'><td style='width:50%;'>库存数：" + list[i].qty + "</td><td style='width:50%;'>批次号：" + list[i].lotCode + "</td></tr> ";
                        html += "<tr style='width:100%;'><td style='width:50%;'>仓库编码：" + list[i].workCenterCode + "</td><td style='width:50%;'>库位编码：" + list[i].workCellCode + "</td></tr> ";
                        html += "</table></div>";
                    }
                    $api.html($api.byId("tbodyHtml"), html);
                    //失败
                } else if (myobj.code == "1") {

                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }


    //保存按钮
    function save() {
        //库位编码
        var workCellCode = $("#workCellCode").val();
        //物料号
        var mrlCode = $("#mrlCode").val();
        //批次号
        var lotCode = $("#lotCode").val();
        //入库数量
        var qty = $("#qty").val();


        //库位非空校验
        if (workCellCode == null || workCellCode == "" || workCellCode == undefined) {
            api.toast({
                msg: '请扫描或选择库位',
                duration: 3000
            });
            return;
        }

        //物料号非空校验
        if (mrlCode == null || mrlCode == "" || mrlCode == undefined) {
            api.toast({
                msg: '请录入物料号',
                duration: 3000
            });
            return;
        }

        //批次号非空校验
        if (lotCode == null || lotCode == "" || lotCode == undefined) {
            api.toast({
                msg: '请录入批次号',
                duration: 3000
            });
            return;
        }

        //批次号正则校验
        var reg = /^[0-9]*[1-9][0-9]*$/;
        if (!reg.test(lotCode)) {
            api.toast({
                msg: '批次号只能是数字格式',
                duration: 3000
            });
            return;
        }

        //入库数量非空校验
        if (qty == null || qty == "" || qty == undefined) {
            api.toast({
                msg: '请录入数量',
                duration: 3000
            });
            return;
        }
        //数量正则校验
        var reg = /^[1-9]\d*$/;
        if (!reg.test(qty)) {
            api.toast({
                msg: '数量只能是正整数',
                duration: 3000
            });
            return;
        }
        //物料号和条码特殊字符处理
        //     mrlCode = encodeURIComponent(encodeURIComponent(mrlCode));
        //      tm = encodeURIComponent(encodeURIComponent(tm));
        api.showProgress({
            title: '保存中...',
        });

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/spareInOfStock";
        var params = new Object();
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                workCellGid: workCellGid, //库位编码
                appVersion: api.appVersion,
                mrlCode: mrlCode, //物料号
                lotCode: lotCode, //批次号
                qty: qty, //转移数量
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 60,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    api.hideProgress();

                    //清空表单
                    api.toast({
                        msg: "操作成功",
                        duration: 8000
                    });
                    //刷新表格数据
                    doSearch();
                    //清空表单
                    workCellGid = "";
                    $("#mrlCode").val("");
                    $("#mrlName").val("");
                    $("#lotCode").val("");
                    $("#qty").val("");
                    $("#workCellCode").val("");
                    $("#workCellCode").focus();
                    idSelector = "workCellCode";
                    if (myobj.codeQ == '1') {
                        api.hideProgress();
                        //清空表单
                        api.toast({
                            msg: myobj.message,
                            duration: 5000
                        });
                    }


                } else if (myobj.code == '1') {
                    api.hideProgress();
                    //清空表单
                    api.toast({
                        msg: myobj.message,
                        duration: 5000
                    });
                }
            } else {
                api.hideProgress();
                alert("连接超时，请耐心等待");
            }
        });
    }

    //查询物料编码
    function getMrlCode() {

        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/spareOfStock";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                workCellGid: workCellGid, //库位
                appVersion: api.appVersion,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    var mydata = myobj.list;
                    document.getElementById("mrlCode").value = myobj.mrlCode == undefined ? "" : myobj.mrlCode;
                    document.getElementById("mrlName").value = myobj.mrlName == undefined ? "" : myobj.mrlName;
                    if (myobj.mrlCode != undefined && myobj.mrlCode != null && myobj.mrlCode != '') {
                        timePicker();
                    }
                } else {

                }
            }
        });
    }
    //时间选择器
    function timePicker(id) {
        api.openPicker({
            type: 'date',
            date: new Date(),
            title: '选择日期'
        }, function (ret, err) {
            if (ret) {
                var monthTemp = (ret.month < 10 ? '0' + ret.month : ret.month);
                var dayTemp = (ret.day < 10 ? '0' + ret.day : ret.day);
                var date = ret.year.toString() + monthTemp.toString() + dayTemp.toString();
                $("#lotCode").val(date);
                api.setFocus({
                    inputId: 'qty',
                    focus: true
                })
                // $("#qty").focus();
                // $("#qty").select();
            }
        });
    }
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>